﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntervalTimer
{
    public class Timer
    {
        private int currentHours;
        private int currentMinutes;
        private int currentSeconds;

        private int startHours;
        private int startMinutes;
        private int startSeconds;

        public Tuple<int, int, int> Time
        { 
            get { return new Tuple<int, int, int>(currentHours, currentMinutes, currentSeconds); }
            set { startHours = value.Item1; startMinutes = value.Item2; startSeconds = value.Item3; } 
        }

        private bool running;

        public Timer(int hours, int minutes, int seconds)
        {
            startHours = hours;
            startMinutes = minutes;
            startSeconds = seconds;
        }

        public void StartTimer()
        {
            running = true;

            currentHours = startHours;
            currentMinutes = startMinutes;
            currentSeconds = startSeconds;

            CountDown();
        }

        public void PauseTimer()
        {
            running = false;
        }

        public void UnpauseTimer()
        {
            running = true;
        }

        public void StopTimer()
        {
            running = false;
            currentHours = startHours;
            currentMinutes = startMinutes;
            currentSeconds = startSeconds;
        }

        private void CountDown()
        {
            while (running)
            {
                if (currentSeconds > 0)
                {
                    currentSeconds--;
                }
                else
                {
                    if(currentMinutes > 0)
                    {
                        currentMinutes--;
                        currentSeconds = 60;
                    }
                    else
                    {
                        if(currentHours > 0)
                        {
                            currentHours--;
                            currentMinutes = 60;
                        }
                        else
                        {
                            running = false;
                        }
                    }
                }
            }
        }

        public override string ToString()
        {
            return String.Format("{0:00}:{1:00}:{2:00}", currentHours, currentMinutes, currentSeconds);
        }
    }
}
